package cn.candy.org.controller;

import cn.candy.basic.util.AjaxResult;
import cn.candy.basic.util.PageList;
import cn.candy.org.domain.Department;
import cn.candy.org.query.DepartmentQuery;
import cn.candy.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 部门接口类
 */
@RestController // = @Controller + @ResponseBody[将这个类下所有方法的返回值转成json格式的字符串响应给调用方]
@RequestMapping("/department")
@Api(tags = "部门接口类",description = "部门接口详细描述")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询所有对象接口：
     * @return - 返回查询的结果
     */
    //定义一个方法：返回值：List<Department> +  方法名：findAll + 参数列表：无
    //处理请求：Get http://localhost:8080/department
    //@RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping
    @ApiOperation(value = "查询所有接口",notes = "")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 查询单个对象接口
     * @param id - 传递查询对象的id
     * @return
     */
    //以前处理请求：Get http://localhost:8080/department?id=1
    //处理请求：Get http://localhost:8080/department/1
    @ApiOperation(value = "查询单个接口",notes = "需要传递id")
    @GetMapping("/{id}")   //用{}区分是请求参数，没有用{}是请求地址
    //@PathVariable("id")Long id  -> 从路径上去拿一个名词为id的变量  赋值给  long id
    public Department findOne(@PathVariable("id")Long id){
        return departmentService.findOne(id);
    }

    /**
     * 分页查询接口
     * @param departmentQuery - 分页参数 + 高级查询参数
     * @return 查询结果 - totals data
     */
    //疑问1：为什么要用Post，不是获取吗？ - 如果是Get请求参数会通过地址栏进行传递【一般情况下，获取用Get】
    //疑问2：@RequestBody DepartmentQuery departmentQuery
    //1.post请求的请求参数会出现在请求数据包的请求体中，然后@RequestBody - 获取请求体中的参数
    @ApiOperation(value = "分页查询接口",notes = "需要传递分页数据")
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 删除接口
     * @param id - 传递需要删除的id
     * @return
     */
    @DeleteMapping("/{id}")
    //处理请求：Delete http://localhost:8080/department/1
    @ApiOperation(value = "删除接口",notes = "需要传递id")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败，" + e.getMessage());
        }
    }
    /**
     * 添加或修改接口
     * @param department
     * @return
     */
    @PutMapping
    //处理请求：Put http://localhost:8080/department + 请求参数
    @ApiOperation(value = "添加或修改接口",notes = "需要传递对象")
    public AjaxResult saveOrUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){//添加操作
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败，" + e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids - 需要传递删除对象的id
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口",notes = "需要传递删除对象的id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("批量删除失败，" + e.getMessage());
        }
    }

    /**
     * 加载部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "加载部门树接口",notes = "")
    public List<Department> getDeptTree(){
        return departmentService.getDeptTree();
    }


}
